<?php

class BannersModel extends Banners{
	
	public function insertBanners($arr){
		try {
			$obj = new Banners();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$tot_f = count($arr['files']);
			if($tot_f > 0){
				for($i = 0; $i < $tot_f; $i++){
					$met = 'setImagem'.$i;
					if($arr['files'][$i]['name'] != ''){
						$ext = pathinfo ($arr['files'][$i]['name']);
						$path = $arr['files'][$i]['tmp_name'];
						$nome = md5(microtime()).".".$ext['extension'];
						$new_path = '../imagens/banners/'.$nome;
						if(!move_uploaded_file($path, $new_path)){
							throw new Exception ( "Ocorreu um erro ao salvar a imagem!" );
						}
						$obj->$met($nome);
					}else{
						$obj->$met(0);
					}
				}
			}
			$db = new Db;
			$db->Insert('banners',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateBanners($arr){
		try {
			$obj = new Banners();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$valor = "'".$valor."'";
					$obj->$met($valor);
				}
			}
			$tot_f = count($arr['files']);
			if($tot_f > 0){
				for($i = 0; $i < $tot_f; $i++){
					$met = 'setImagem'.$i;
					if($arr['files'][$i]['name'] != ''){
						$ext = pathinfo ($arr['files'][$i]['name']);
						$path = $arr['files'][$i]['tmp_name'];
						$nome = md5(microtime()).".".$ext['extension'];
						$new_path = '../imagens/banners/'.$nome;
						if(!move_uploaded_file($path, $new_path)){
							throw new Exception ( "Ocorreu um erro ao salvar a imagem!" );
						}
						$obj->$met("'".$nome."'");
					}else{
						$obj->$met($arr['dados']['imagem']);
					}
				}
			}
			$db = new Db;
			$db->Update('banners',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteBanners($id){
		try {
			$db = new Db;
			$db->Delete('banners',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarBanners($parametros){
		try {
			$campos = array('*');
			if($parametros[0] != 'last'){
				$where = 'idbanners = '.$parametros[0];
				$order = '';
			}else{
				$where = 'idcategorias = '.$parametros[1];
				$order = 'idbanners DESC';
			}
			$db = new Db;
			$db->Select('banners',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarBanners($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'idbanners DESC';
			$db = new Db;
			$db->Select('banners',$campos,$where,$order);
			$view = new BannersView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function dropdownBanners($parametros){
		try {
			$campos = array('*');
			$where = '';
			$order = 'idbanners DESC';
			$db = new Db;
			$db->Select('banners',$campos,$where,$order);
			$view = new BannersView();
			$this->lista = $view->select($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function visualizarBanners($parametros){
		try {
			$campos = array('*');
			$where = 'idlocal = '.$parametros;
			$order = 'ordem ASC';
			$db = new Db;
			$db->Select('banners',$campos,$where,$order);
			$view = new BannersView();
			$this->lista = $view->visualizar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>